from os import path

from PIL import Image

import numpy as np

import matplotlib.pyplot as plt

from wordcloud import WordCloud, STOPWORDS

import random

import jieba


def grey_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
    return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)


d = path.dirname(__file__)

# Read the whole text.

text = open(path.join(d, 'text/alice.txt')).read()

content = jieba.lcut(text)

words = ' '.join(content)

# read the mask image

# taken from

# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg

alice_mask = np.array(Image.open(path.join(d, "img/qingwa.jpg")))

wc = WordCloud(max_words=8000, mask=alice_mask, margin=10, random_state=1, max_font_size=15, min_font_size=2, scale=10)

# generate word cloud

wc.generate(words)

default_colors = wc.to_array()

# store to file

# show
plt.title('Custom colors')

plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3))

wc.to_file(path.join(d, "alice.png"))

plt.axis("off")

plt.figure()

plt.title('Default colors')

plt.imshow(default_colors)

plt.axis("off")

plt.show()
